package com.study.cloud.apilog.request;

import com.study.cloud.apilog.GatewayApiLog;
import com.study.cloud.utils.MediaTypeUtil;
import com.study.cloud.utils.SplitStringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.util.Map;

/**
 * @Date 2022/5/17
 * @Author Cliff
 * desc
 */
@Component
public class SimpleRequestBodyImpl extends AbstractRequestBody{

    private Logger logger = LoggerFactory.getLogger(SimpleRequestBodyImpl.class);

    @Override
    public boolean isSupport(MediaType mediaType) {
        return !(mediaType != null
                && (MediaTypeUtil.isFormDataType(mediaType)
                || MediaTypeUtil.isWWWFormType(mediaType)
                || MediaTypeUtil.isApplicationJsonType(mediaType)));
    }

    @Override
    public BodyWrapper getRequestBody(ServerWebExchange exchange, GatewayApiLog gatewayLog) {
        // 获取请求参数
        Map<String, String> queryParams = exchange.getRequest().getQueryParams().toSingleValueMap();
        String urlParamsByMap = SplitStringUtil.getUrlParamsByMap(queryParams);
        gatewayLog.setRequestBody(urlParamsByMap);
        BodyWrapper wrapper = new BodyWrapper();
        wrapper.setRequestBody(urlParamsByMap);
        return  wrapper;
    }

}
